package com.i8office.android.zhihuixinwu.module.presenter;

import android.text.TextUtils;

import com.cyan.tools.callback.SimpleCallback;
import com.i8office.android.zhihuixinwu.bean.Code;
import com.i8office.android.zhihuixinwu.bean.People;
import com.i8office.android.zhihuixinwu.bean.SendInfo;
import com.i8office.android.zhihuixinwu.module.contract.StaffContract;
import com.i8office.android.zhihuixinwu.module.model.StaffModel;
import com.socks.library.KLog;

import java.util.ArrayList;
import java.util.Map;

/**
 * Created by pc on 2018/7/8.
 */

public class StaffPresenter extends BasePresenterImpl<StaffContract.View> implements StaffContract.Presenter {
    StaffContract.Model model;
    public StaffPresenter(StaffContract.View view) {
        super(view);
        model = new StaffModel();
    }

    @Override
    public void getSendInfo(Map<String, String> map) {
        addDisposable(model.getSendInfo(new SimpleCallback<SendInfo>(mView) {
            @Override
            public void requestSuccess(SendInfo data) {
                mView.showResults(data);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }
        }, map));
    }

    @Override
    public void searchPeople(Map<String, String> map) {
        addDisposable(model.searchPeople(new SimpleCallback<ArrayList<People>>(mView) {
            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }

            @Override
            public void requestSuccess(ArrayList<People> data) {
                super.requestSuccess(data);
                mView.showPeoples(data);
            }
        }, map));
    }

    @Override
    public void checkBeforeSubmit(Map<String, String> map) {
        addDisposable(model.checkBeforeSubmit(new SimpleCallback<Code>(mView) {
            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.requestError();
            }

            @Override
            public void requestSuccess(Code data) {
                super.requestSuccess(data);
                if (data.isSucceed()) {
                    mView.checkResult();
                }else {
                    if(!TextUtils.isEmpty(data.getMessage())){
                        mView.toast(data.getMessage());
                    }
                    mView.requestError();
                }
            }
        }, map));
    }

    @Override
    public void submit(Map<String, String> map) {
        addDisposable(model.send(new SimpleCallback<Code>(mView) {
            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.requestError();
            }

            @Override
            public void requestSuccess(Code data) {
                super.requestSuccess(data);
                if (data.isSucceed()) {
                    mView.submitResult();
                }else {
                    if(!TextUtils.isEmpty(data.getMessage())){
                        mView.toast(data.getMessage());
                    }else{
                        mView.toast(data.getMessage());
                    }
                    mView.requestError();
                }

            }
        }, map));
    }

    @Override
    public void open(Map<String, String> map) {
        addDisposable(model.open(new SimpleCallback<String>(mView) {
            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.openResult();
                mView.requestError();
            }

            @Override
            public void requestSuccess(String data) {
                super.requestSuccess(data);
                mView.openResult();
            }
        }, map));
    }

    @Override
    public void sendNotification(Map<String, String> map) {
        model.sendNotification(new SimpleCallback<String>(mView){
            @Override
            public void beforeRequest() {

            }

            @Override
            public void requestComplete() {
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                KLog.e("发送消息失败"+msg);
            }

            @Override
            public void requestSuccess(String data) {
                super.requestSuccess(data);
                KLog.e("发送消息成功"+data);
            }
        },map);
    }
}
